// pages/golddiscounts/couponDetail/index.js
const service = require("../../../service/port.js");
const serviceactivity = require("../../../service/activity.js");
import Toast from '../../../miniprogram_npm/@vant/weapp/toast/toast.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showtip: false,
    getit: false,
    outline: false,
    listdata: [],
    name: '',
    startTime: '',
    endTime: '',
    type: 1,
    dicount: '',
    exchaneContent: '',
    isReceive: 0,
    CouponId: 0,
    reduceMoney: 0,
    satifyMoney: 0
  },
  switchtip() {
    this.setData({
      showtip: !this.data.showtip
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let self = this
    wx.showNavigationBarLoading(); //在标题栏中显示加载
    self.setData({
      CouponId: options.CouponId,
      type: options.type
    })

    if (options.type == 4) {
      let param = {
        "data": {
          //礼品名称
          "name": "",
        },
        //分页
        "queryRequest": {
          //页码
          "pageNum": 1,
          //面数据量
          "pageSize": 1000
        }
      }
      serviceactivity.getLPList(param, (res) => {
        if (res.code == "00000") {
          self.setData({
            listdata: res.data.rows,
          })
          wx.hideNavigationBarLoading();
          wx.stopPullDownRefresh();
        }
      })


      return
    }

    serviceactivity.getCouponDetailes({
      "data": {
        "couponId": self.data.CouponId, //优惠券id
        "cutomerId": wx.getStorageSync('userInfo').id //客户id
      }
    }, res => {
      console.log(res.data)
      if (res.code == "00000") {
        self.setData({
          name: res.data.CouponDetailes.name ? res.data.CouponDetailes.name : '',
          startTime: res.data.CouponDetailes.startTime.trim().split(" ")[0],
          endTime: res.data.CouponDetailes.endTime.trim().split(" ")[0],
          listdata: res.data.CouponSkuDetailes,
          type: res.data.CouponDetailes.type,
          dicount: res.data.CouponDetailes.dicount,
          exchaneContent: res.data.CouponDetailes.exchaneContent,
          isReceive: res.data.CouponDetailes.isReceive,
          reduceMoney: res.data.CouponDetailes.reduceMoney,
          satifyMoney: res.data.CouponDetailes.satifyMoney
        })
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
      }
    })
  },
  addCartTo(e) {
    // console.log(e)
    service.addcart({
      customerId: wx.getStorageSync('userInfo').id,
      spuId: e.currentTarget.dataset.index.spuId,
      skuId: e.currentTarget.dataset.index.id,
      num: 1
    }, res => {
      wx.showToast({
        title: '添加成功',
        icon: 'success',
        duration: 2000
      })
    })
  },
  receiveCouponTo() {
    let self = this;
    serviceactivity.getReceiveCoupon({
      "data": {
        "couponId": self.data.CouponId, //优惠券id
        "cutomerId": wx.getStorageSync('userInfo').id //客户id
      }
    }, res => {
      // console.log(res)

      if (res.code == "00000") {
        wx.showToast({
          title: '领取成功',
          icon: 'success',
          duration: 2000
        })
        self.setData({
          isReceive: 1
        })
      } else if (res.code == "H1300") {
        Toast.fail(res.message);
      }
    })
  },
  gooddetailTo(e) {
    // console.log(e)
    let type = this.data.type
    if (type == 4) return;
    wx.navigateTo({
      url: "/pages/product/productDetail/index?id=" + e.currentTarget.dataset.index.spuId
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})